/*******************************************************************************
 * Copyright (C) 2014 The Calrissian Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package org.calrissian.restdoclet.writer.swagger.model;

import static org.calrissian.restdoclet.util.CommonUtils.isEmpty;
import static org.calrissian.restdoclet.util.TypeUtils.dataType;
import static org.calrissian.restdoclet.util.TypeUtils.getTypeName;

import java.util.Collection;

import org.calrissian.restdoclet.model.ModelRef;

import com.sun.javadoc.Type;

public class Operation {
    private final String                method;
    private final String                nickname;
    private final String                notes;
    private final String                summary;
    private final String                type;
    private final ModelRef              items;
    private final Collection<String>    produces;
    private final Collection<String>    consumes;
    private final Collection<Parameter> parameters;

    public Operation(String method, String nickname, String notes, String summary, Type type,
                     Collection<String> produces, Collection<String> consumes,
                     Collection<Parameter> parameters) {
        this.method = method;
        this.nickname = nickname;
        this.notes = notes;
        this.summary = summary;
        this.type = dataType(type);
        if (this.type.startsWith("array")) {
            Type arrayItemtype = type.asParameterizedType().typeArguments()[0];
            ModelRef fieldModelRef = new ModelRef();
            fieldModelRef.setType(getTypeName(arrayItemtype));
            this.items = fieldModelRef;
        } else {
            this.items = null;
        }
        this.produces = (isEmpty(produces) ? null : produces);
        this.consumes = (isEmpty(consumes) ? null : consumes);
        this.parameters = (isEmpty(parameters) ? null : parameters);
    }

    public String getMethod() {
        return method;
    }

    public String getNickname() {
        return nickname;
    }

    public String getNotes() {
        return notes;
    }

    public String getSummary() {
        return summary;
    }

    public String getType() {
        return type;
    }

    public Collection<String> getProduces() {
        return produces;
    }

    public Collection<String> getConsumes() {
        return consumes;
    }

    public Collection<Parameter> getParameters() {
        return parameters;
    }

    public ModelRef getItems() {
        return items;
    }
}
